using UnityEngine;
using System.Collections;

[RequireComponent(typeof(GUITexture))]
public class ButtonControl : MonoBehaviour
{
    #region Attributes

    public Texture2D normalTexture;
    public Texture2D overTexture;
    public Texture2D downTexture;
    public Texture2D disabledTexture;

    private ButtonHandler buttonHandler;

    [SerializeField]
    private bool btnEnabled = true;

    #endregion

    #region Props
  
    public bool BtnEnabled
    {
        get { return this.btnEnabled; }
        set
        { 
            this.btnEnabled = value;
            this.ChangeTexture(this.normalTexture);
        }

    }

    #endregion

    #region Methods

    private void ChangeTexture(Texture2D texture)
    {
        if (this.btnEnabled)
            this.guiTexture.texture = texture;
        else
            this.guiTexture.texture = this.disabledTexture;
    }

    #endregion

    #region Unity Methods
    void Start()
    {
        this.buttonHandler = this.GetComponent<ButtonHandler>();
        this.ChangeTexture(this.normalTexture);
    }

    void OnMouseOver()
    {
        if (!Input.GetMouseButton(InputConstants.LEFT_CLICK))
            this.ChangeTexture(this.overTexture);
    }

    void OnMouseExit()
    {
        this.ChangeTexture(this.normalTexture);
    }

    void OnMouseDown()
    {
        this.ChangeTexture(this.downTexture);
    }

    void OnMouseUpAsButton()
    {
        this.ChangeTexture(this.overTexture);
        if (this.btnEnabled)
            this.buttonHandler.OnClick();
    }

    #endregion
}
